package com.bella.api.system.controller;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zsq
 * @create 2020-06-12-12:20
 */
public class Test {
    public static void main(String[] args) {
        try {
            BufferedReader fr = new BufferedReader(new FileReader("E:\\coupon\\coupon.txt"));
            String contentLine = fr.readLine();
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.append("INSERT INTO `market_db`.`mk_user_coupon` (`user_id`, `coupon_id`, `give_count`, `appointment_shop_id`, `redeem_code`, `user_coupon_code`, `create_type`, `status`, `use_date_from`, `use_date_to`, `display_date_to`, `activation_time`, `use_date`, `use_shop_id`, `saved_money`, `pos_no`, `serial_number`, `create_time`, `update_time`, `version`) VALUES");

            while (contentLine != null) {
                stringBuilder2.append(" ('"+contentLine+"', '22193', '0', NULL, NULL, NULL, 'PICK', 'CREATED', '2020-06-15 12:00:00', '2020-06-28 23:59:59', '2020-09-26 23:59:59', NULL, NULL, NULL, NULL, NULL, NULL, '2020-06-15 12:00:00', '2020-06-15 12:00:00', '0'),");
                stringBuilder2.append("\r\n");
                contentLine = fr.readLine();
            }
            String a  = stringBuilder2.toString().substring(0,stringBuilder2.toString().length()-1)+";";
            System.out.println(a);
            generateCsv("E:\\coupon\\coupon2.csv","test",a);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static File generateCsv(String filePath, String fileName,String sql) throws IOException {
        File csvFile = null;
        BufferedWriter csvWrite = null;
        try {
            csvFile = new File(filePath + File.separator + fileName + ".csv");
            //去文件目录
            File parent = csvFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            boolean flg = true;
            if (csvFile.exists()) {
                flg = false;
            } else {
                csvFile.createNewFile();
            }
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(csvFile, true), "UTF-8");
            //以UTF-8无BOM格式编码(utf-8生成csv文件用excel打开乱码)
            osw.write(new String(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}));
            csvWrite = new BufferedWriter(osw, 1024);
            //写入数据
            write(csvWrite,sql);
            csvWrite.flush();

        } catch (IOException e) {
            throw new IOException("文件生成失败");
        } finally {
            try {
                csvWrite.close();
            } catch (IOException e) {
                throw new IOException("关闭文件流失败");
            }
        }
        return csvFile;
    }

    /**
     * 将数据按行写入数据
     *
     * @param dataList
     * @param csvWrite
     * @throws IOException      
     */
    private static void write(BufferedWriter csvWrite,String sql)
            throws IOException {
        StringBuffer buffer = new StringBuffer();
        buffer.append(sql);
        String rowStr = buffer.toString();
        csvWrite.write(rowStr);
        csvWrite.newLine();
    }

}
